<?php

namespace App\Http\Controllers\Api;


use App\Models\Product;
use App\Services\ProductService;
use Illuminate\Http\Request;

class ProductsController extends Controller
{
    // 商品列表
    public function index(Request $request)
    {
        $builder = Product::query()->where('on_sale', true);

        // 模糊查询
        if ($search = $request->input('search', '')) {
            $like = '%' . $search . '%';
            $builder = $builder->where(function ($query) use ($like) {
                $query->where('title', 'like', $like)
                    ->orWhere('description', 'like', $like)
                    ->orWhereHas('skus', 'like', function ($query) use ($like) {
                        $query->where('title', 'like', $like)
                            ->orWhere('description', 'like', $like);
                    });
            });
        }

        // 是否有order参数
        if ($order = $request->input('order', '')) {
            if (preg_match('/^(.+)_(asc|desc)$/', $order, $m)) {
                if (in_array($m[1], ['price', 'sold_count', 'rating'])) {
                    // 根据参数进行排序
                    $builder = $builder->orderBy($m[1], $m[2]);
                }
            }
        }

        // 根据category搜索商品
        if ($category_id = $request->input('cate_id', '')) {
            $builder = $builder->where('category_id', $category_id);
        }

        // 根据品牌搜索商品
        if ($brand_id = $request->input('brand_id', '')) {
            $builder = $builder->where('brand_id', $brand_id);
        }

        $products = $builder->simplePaginate(10);

        return $this->success($products);
    }

    // 商品详情  例子: http://demo.rageframe.com/api/tiny-shop/v1/product/product/view?id=68
    public function show(Product $product, ProductService $productService)
    {
        // 判断商品是否上架
        if (!$product->on_sale) {
            return $this->failed('商品未上架');
        }

        // 商品查看量
        $product->view_count ++;
        $product->save();

        return $this->success($productService->showProduct($product, true));
    }

    // 收藏商品
    public function favour(Product $product, Request $request)
    {
        $user = $request->user();

        if ($user->favoriteProducts()->find($product->id)) {
            return $this->message('商品收藏成功');
        }

        $user->favoriteProducts()->attach($product);

        return $this->message('商品收藏成功');
    }

    // 取消收藏商品
    public function disfavour(Product $product, Request $request)
    {
        $user = $request->user();
        $user->favoriteProducts()->detach($product);

        return $this->message('商品取消收藏成功');
    }

    // 收藏商品列表
    public function favourites(Request $request)
    {
        $products = $request->user()->favoriteProducts()->paginate(10);
        return $this->success($products);
    }

    // 热门商品
    public function hotIndex()
    {
        $products = Product::query()->where('on_sale', true)
            ->orderBy('sold_count', 'desc')
            ->get()
            ->take(10);
        return $this->success($products);
    }

    // 最新商品
    public function newIndex()
    {
        $products = Product::query()->where('on_sale', true)
            ->orderBy('created_at', 'desc')
            ->get()
            ->take(10);
        return $this->success($products);
    }

    // 推荐商品
    public function recommendIndex()
    {
        $products = Product::query()->where('on_sale', true)
            ->inRandomOrder()
            ->get()
            ->take(10);
        return $this->success($products);
    }

    // 猜你喜欢商品
    public function guessYouLikeIndex()
    {
        $products = Product::query()->where('on_sale', true)
            ->inRandomOrder()
            ->paginate(4);
        return $this->success($products);
    }
}
